﻿using Games.Common;
using System;
using Xamarin.Forms;

namespace Games.DigitalSame
{
    class GameCell : Label
    {
        public event EventHandler Tapped;

        public GameCell()
        {
            this.BackgroundColor = Color.BurlyWood;
            this.TextColor = Color.Black;
            this.TextType = TextType.Html;
            this.HorizontalTextAlignment = TextAlignment.Center;
            this.VerticalTextAlignment = TextAlignment.Center;
            this.FontAttributes = FontAttributes.Bold;

            TapGestureRecognizer tapGesture = new TapGestureRecognizer { NumberOfTapsRequired = 1 };
            tapGesture.Tapped += (sender, args) =>
            {
                Tapped?.Invoke(this, EventArgs.Empty);
            };
            GestureRecognizers.Add(tapGesture);
        }
        public void InitValue(int value, int click = 0)
        {
            this.Value = value;
            this.Click = click;
            this.SetString();
        }

        public int Col { set; get; }

        public int Row { set; get; }
        public int Value { get; private set; }
        public int Click { get; private set; }

        public void Grow(bool onclick = false)
        {
            this.Value++;
            if(this.Value > 9)
            {
                this.Value = 1;
            }
            if(onclick)
            {
                this.Click++;
            }
            this.SetString();
        }

        private void SetString()
        {
            var formattedString = new FormattedString();
            formattedString.Spans.Add(new Span
            {
                Text = this.Value.ToString(),
                ForegroundColor = Color.Black,
                FontAttributes = FontAttributes.Bold,
                FontSize = this.FontSize
            });
            if(Utils.NoMaxLimit)
            {
                formattedString.Spans.Add(new Span
                {
                    Text = this.Click.ToString(),
                    ForegroundColor = Color.SlateBlue,
                    FontSize = 12
                });
            }
            this.FormattedText = formattedString;
        }
    }
}
